/**
 * @author zhounan 
 * @date 2024年6月26日
 * @version V1.0
 */
package org.zn.etl.misc;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author zhounan
 * @date 2024年6月26日
 * @version V1.0
 */

public class ExpressionHelper {

	private ExpressionParser parser;
	private ParserContext parserContext;

	@PostConstruct
	public void init() {
		parser = new SpelExpressionParser();
		parserContext = new TemplateParserContext("${", "}");
	}

	public Expression parseExpression(String expStr) {

		Expression exp = parser.parseExpression(expStr, parserContext);

		return exp;

	}

	public static EvaluationContext map2EvalContext(Map<String, Object> map) {

		EvaluationContext evalContext = new StandardEvaluationContext();

		for (Map.Entry<String, Object> ent : map.entrySet()) {

			evalContext.setVariable(ent.getKey(), ent.getValue());
		}
		return evalContext;

	}
	
	

}
